<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()" >刷新</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <!--表格-->
  <a-table :dataSource="passengers"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'type'">
        <span v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.type">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <!--添加表单-->
  <a-modal v-model:visible="visible" title="乘车人" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="passenger" :label-col="{span: 4}" :wrapper-col="{ span: 20 }">
      <a-form-item label="姓名">
        <a-input v-model:value="passenger.name" />
      </a-form-item>
      <a-form-item label="身份证">
        <a-input v-model:value="passenger.idCard" />
      </a-form-item>
      <a-form-item label="旅客类型">
        <a-select v-model:value="passenger.type">
          <a-select-option v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import request from "@/utils/request";
import {notification} from 'ant-design-vue'

// 旅客类型枚举
const PASSENGER_TYPE_ARRAY = window.PASSENGER_TYPE_ARRAY;
// 新增按钮隐藏属性
const visible = ref(false);
// 表格加载
let loading = ref(false);
// 添加乘车人的属性
let passenger = ref({
  id: undefined,
  memberId: undefined,
  name: undefined,
  idCard: undefined,
  type: undefined,
  createTime: undefined,
  updateTime: undefined,
});
// 表格数据
let passengers = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  position: ['bottomCenter'],
  total: 0,
  current: 1,
  pageSize: 10,
  showSizeChanger: true,
  pageSizeOptions: ['10']
});
// 表格展示列
const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '身份证',
    dataIndex: 'idCard',
    key: 'idCard',
  },
  {
    title: '旅客类型',
    dataIndex: 'type',
    key: 'type',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  }
];

// 点击添加按钮，表单显示
const onAdd = () => {
  passenger.value = {};
  visible.value = true;
};

// 编辑乘车人
const onEdit = (record) => {
  passenger.value = window.Tool.copy(record);
  visible.value = true;
};

// 删除乘车人
const onDelete = (record) => {
  request.delete(`/member/passenger/delete/${record.id}`)
      .then((result) => {
        if (result.status) {
          notification.success({description: result.message});
          handleQuery({
            pageNum: pagination.value.current,
            pageSize: pagination.value.pageSize,
          });
        } else {
          notification.error({description: result.message});
        }
  });
};

// 请求 -- 保存乘车人
const handleOk = () => {
  request.post("/member/passenger/save", passenger.value)
    .then((result) => {
      if (result.status) {
        notification.success({description: result.message});
        visible.value = false;
        handleQuery({
          pageNum: pagination.value.current,
          pageSize: pagination.value.pageSize
        });
      } else {
        notification.error({description: result.message});
      }
  });
}

// 请求 -- 查询乘车人列表
const handleQuery = (param) => {
  // 如果无参，则查询当前页
  if (!param) {
    param = {
      pageNum: pagination.value.current,
      pageSize: pagination.value.pageSize
    }
  }
  loading.value = true;
  request.get("/member/passenger/list", {
    params: {
      pageNum: param.pageNum,
      pageSize: param.pageSize
    }
  }).then((result) => {
    loading.value = false;
    if (result.status) {
      passengers.value = result.content.records;
      // 设置分页控件的值
      pagination.value.current = param.pageNum;
      pagination.value.total = result.content.total;
    } else {
      notification.error({description: result.message});
    }
  });
};

// 表格换页，调用请求
const handleTableChange = (page) => {
  pagination.value = page
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize,
  });
};

onMounted(() => {
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
});
</script>
